package com.team.arms.bingyiInfo.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.arms.bingyiInfo.entity.dto.AdvanceDTO;
import com.team.arms.bingyiInfo.entity.dto.ConditionDTO;
import com.team.arms.bingyiInfo.entity.vo.BingyiInfoVO;
import com.team.arms.common.PageVO;
import com.team.arms.common.Result;
import com.team.arms.common.Result2;
import com.team.arms.common.annotation.Log;
import com.team.arms.common.ry.core.controller.BaseController;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.bingyiInfo.entity.BingyiInfo;
import com.team.arms.bingyiInfo.entity.dto.BingyiInfoDTO;
import com.team.arms.bingyiInfo.service.IBingyiInfoService;
import com.team.arms.common.ry.core.page.TableDataInfo;
import com.team.arms.common.ry.enums.BusinessType;
import com.team.arms.common.ry.utils.poi.ExcelUtil;
import com.team.arms.common.util.ConvertUtil;
import com.team.arms.political.domain.PoliReviewInfo;
import com.team.arms.political.domain.vo.Polivo;
import com.team.arms.political.service.PoliReviewInfoService;
import com.team.arms.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WY
 * @since 2022-05-04
 */
@RestController
@RequestMapping("/military")
@Api(tags="兵役信息模块")
@CrossOrigin
public class BingyiInfoController extends BaseController {
    @Autowired
    private IBingyiInfoService iBingyiInfoService;

    @Autowired
    private PoliReviewInfoService poliReviewInfoService;

    @Autowired
    private ConvertUtil convertUtil;

//    @GetMapping("/hello")
//    public String test(){
//        return "hello,Arms";
//    }
//
//    @GetMapping("{id}")
//    public AjaxResult test1(@PathVariable String id){
//        return AjaxResult.success(iBingyiInfoService.getById(id));
//    }

    @GetMapping("/getList")
    @ApiOperation("分页展示兵役信息")
    public Result2 listBinyiInfo(@RequestParam("page")Integer pageNum, @RequestParam("pageSize")Integer pageSize){
        QueryWrapper<BingyiInfo> wrapper = new QueryWrapper();
        List<BingyiInfo> messages = iBingyiInfoService.list();
        for(BingyiInfo bingyiInfo:messages){
            System.out.println(bingyiInfo.toString());
        }

        //转换效率的问题,直接sql查询返回这个类型效率相对高一些
        List<BingyiInfoVO> bingyiInfoVos=  convertUtil.convert(messages, BingyiInfoVO.class);
        for (BingyiInfoVO vo:bingyiInfoVos){
            String politicalStatus = iBingyiInfoService.selectPoliticalStatusByBingyiId(vo.getBingyiInfoId());
            vo.setPoliStatus(politicalStatus);
        }

        Page page = PageUtil.list2Page(bingyiInfoVos, pageNum, pageSize);
        return page !=null ? Result2.success("分页成功").data(page): Result2.error("分页失败");
    }

    @PostMapping("/add")
    @ApiOperation("新增兵役信息")
    public AjaxResult getBingyiInfo(@RequestBody BingyiInfoDTO bingyiInfoDTO){
        return toAjax(iBingyiInfoService.insertBingyiInfo(bingyiInfoDTO));
    }

    @DeleteMapping("/removeById/{id}")
    @ApiOperation("删除单个兵役信息")
    public AjaxResult removeById(@PathVariable String id){
        return toAjax(iBingyiInfoService.removeById(id));
    }

    @DeleteMapping("/removeByIds")
    @ApiOperation("批量删除兵役信息")
    public AjaxResult removeByIds(@RequestBody String[] ids){
        ArrayList<String> list = new ArrayList<String>();
        for(String id : ids){
            list.add(id);
        }
        return toAjax(iBingyiInfoService.removeBatchByIds(list));
    }

    @PutMapping("/update")
    @ApiOperation("修改兵役信息")
    public AjaxResult updateBinyiInfo(@RequestBody BingyiInfoDTO bingyiInfoDTO){
        System.out.println(bingyiInfoDTO.toString());
        return toAjax(iBingyiInfoService.updateBinyiInfo(bingyiInfoDTO));
    }

//    @PostMapping("/select")
//    @ApiOperation("条件查询兵役信息")
//    public AjaxResult selectBinyiInfo(@RequestBody ConditionDTO[] conditionDTO){
//        List<BingyiInfoVO> bingyiInfoVOS = iBingyiInfoService.selectBingyiInfo(conditionDTO);
//        return AjaxResult.success(bingyiInfoVOS);
//    }

    @PostMapping("/select/name")
    @ApiOperation("根据姓名查询兵役信息")
    public AjaxResult selectByName(@RequestParam String name){
        QueryWrapper query = new QueryWrapper();
        query.like("name",name);
        List<BingyiInfo> listBingyiInfo = iBingyiInfoService.list(query);
        List<BingyiInfoVO> bingyiInfoVos=  convertUtil.convert(listBingyiInfo, BingyiInfoVO.class);
//        bingyiInfoVos.forEach(System.out::println);
        return AjaxResult.success(bingyiInfoVos);
    }

    @ApiOperation("高级查询兵役信息")
    @PostMapping("/select/advance")
    public AjaxResult selectAdvance(@RequestParam(required = false) String name,@RequestParam(required = false) String sex,@RequestParam(required = false) String age,@RequestParam(required = false) String idCard){
        //要实现的是查询两个字段
        //只and查询这两个字段的
        //其他字段则不管 允许为null
        QueryWrapper<BingyiInfo> query=new QueryWrapper();
//        query.like("name",name)
//                        .or().like("sex",sex)
//                        .or().like("age",age)
//                        .or().like("id_card",idCard);
        if(name!=null){
            query.like("name",name);
        }

        if(sex!=null){
            query.like("sex",sex);
        }

        if (age!=null){
            query.like("age",age);
        }

        if(idCard!=null){
            query.like("id_card",idCard);
        }

        List<BingyiInfo> listBingyiInfo = iBingyiInfoService.list(query);
        List<BingyiInfoVO> bingyiInfoVos=  convertUtil.convert(listBingyiInfo, BingyiInfoVO.class);

        for (BingyiInfoVO vo:bingyiInfoVos){
            String politicalStatus = iBingyiInfoService.selectPoliticalStatusByBingyiId(vo.getBingyiInfoId());
            vo.setPoliStatus(politicalStatus);
        }
        return AjaxResult.success(bingyiInfoVos);
    }

    @ApiOperation("单个条件查询兵役信息")
    @PostMapping("/select/condition")
    public AjaxResult selectByCodition(@RequestParam String condition,@RequestParam String content){
        QueryWrapper<BingyiInfo> query=new QueryWrapper();
        if (condition.equals("姓名")){
            query.like("name",content);
        }else if (condition.equals("年龄")){
            query.like("age",content);
        }else if (condition.equals("身份证")){
            query.like("id_card",content);
        }else if (condition.equals("性别")){
            query.like("sex",content);
        }

        List<BingyiInfo> list = iBingyiInfoService.list(query);
        List<BingyiInfoVO> bingyiInfoVos=  convertUtil.convert(list, BingyiInfoVO.class);
        for (BingyiInfoVO vo:bingyiInfoVos){
            String politicalStatus = iBingyiInfoService.selectPoliticalStatusByBingyiId(vo.getBingyiInfoId());
            vo.setPoliStatus(politicalStatus);
        }
        return AjaxResult.success(bingyiInfoVos);
    }

//    @ApiOperation("高级查询兵役信息")
//    @PostMapping("/select/Advance")
//    public TableDataInfo selectAdvance(@RequestBody(required = false) AdvanceDTO advanceDTO){
//        startPage();
//        QueryWrapper<BingyiInfo> query=new QueryWrapper();
//        query.like("name",advanceDTO.getName())
//                        .or().like("sex",advanceDTO.getSex())
//                        .or().like("age",advanceDTO.getAge())
//                        .or().like("id_card",advanceDTO.getIdCard());
//        List<BingyiInfo> listBingyiInfo = iBingyiInfoService.list(query);
//        List<BingyiInfoVO> bingyiInfoVos=  convertUtil.convert(listBingyiInfo, BingyiInfoVO.class);
//        return getDataTable(bingyiInfoVos);
//    }

    @ApiOperation(("获取详细的兵役信息"))
    @GetMapping("/getInfo/{id}")
    public AjaxResult getBingyiInfo(@PathVariable String id){
        return AjaxResult.success(iBingyiInfoService.getById(id));
    }

    @ApiOperation("导出兵役信息列表")
    @Log(title = "兵役信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //这里URLEncoder.encode可以防止中文乱码
        String fileName = "test1";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //查询数据库
        List<BingyiInfo> bingyiInfolist = iBingyiInfoService.list();
//        List<BingyiInfoVO> bingyiInfoVOS=new ArrayList<BingyiInfoVO>();
//        for (BingyiInfo bingyiInfo:bingyiInfolist){
//            //获取政治面貌
//            QueryWrapper<PoliReviewInfo> queryWrapperPolitical=new QueryWrapper<>();
//            queryWrapperPolitical.eq("poli_info_id",bingyiInfo.getZPoliticalInfoId())
//                    .select("poli_status")
//                    .last("limit 1");
//            PoliReviewInfo poliReviewInfo = poliReviewInfoService.getOne(queryWrapperPolitical);
//            if (poliReviewInfo==null){
//                poliReviewInfo=new PoliReviewInfo();
//            }
//
//            BingyiInfoVO bingyiInfoVO=new BingyiInfoVO(bingyiInfo.getName(),bingyiInfo.getSex(),bingyiInfo.getBeforeName(),bingyiInfo.getBirthday(),bingyiInfo.getAge(),bingyiInfo.getNation(),bingyiInfo.getIsRegister(),poliReviewInfo.getPoliStatus(),bingyiInfo.getEducation(),bingyiInfo.getMarriageCondition());
//            bingyiInfoVOS.add(bingyiInfoVO);
//        }
//        System.out.println(bingyiInfoVOS);
//            List<DovecoteDailyVo> dovecoteDailyVoList = ConvertUtil.java.convert(dovecoteDailies, DovecoteDailyVo.class);
        //调用方法进行写操作
        try {
            EasyExcel.write(response.getOutputStream(), BingyiInfo.class).sheet("兵役信息数据")
                    .doWrite(bingyiInfolist);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("导入兵役信息")
    @PostMapping("/importMilitary")
    @ResponseBody
    public Result importStudent(MultipartFile file) throws Exception
    {
        return iBingyiInfoService.importExcel(file);
    }

}

